package cn.wolfcode.trip.app.controller;

import cn.wolfcode.trip.base.domain.Travel;
import cn.wolfcode.trip.base.domain.TravelReview;
import cn.wolfcode.trip.base.query.TravelCommendQueryObject;
import cn.wolfcode.trip.base.query.TravelQueryObject;
import cn.wolfcode.trip.base.query.TravelReviewQueryObject;
import cn.wolfcode.trip.base.service.ITravelCommendService;
import cn.wolfcode.trip.base.service.ITravelReviewService;
import cn.wolfcode.trip.base.service.ITravelService;
import cn.wolfcode.trip.base.util.JSONResult;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 游记资源控制器
 */
@RestController
@RequestMapping("travels")
public class TravelController {
    @Autowired
    private ITravelService travelService;
    @Autowired
    private ITravelCommendService travelCommendService;
    @Autowired
    private ITravelReviewService travelReviewService;

    /**
     * 查询所有已发布的游记
     * @param qo
     * @return
     */
    @GetMapping
    public PageInfo list(TravelQueryObject qo){
        //1.根据发布时间倒序排列
        qo.setOrderBy("t.releaseTime desc");
        //2.状态为已发布
        return travelService.query(qo);
    }

    /**
     * 新增
     * @return
     */
    @PostMapping
    public JSONResult save(Travel travel) {
        travelService.saveOrUpdate(travel);
        return new JSONResult();
    }

    /**
     * 查询单个游记
     * @param id
     * @return
     */
   @GetMapping("{id}")
    public Travel getById( @PathVariable Long id){
        return travelService.selectById(id);
    }


    /**
     * 更新
     * @return
     */
    @PutMapping("{id}")
    public JSONResult update(Travel travel) {
        travelService.saveOrUpdate(travel);
        return new JSONResult();
    }

    @GetMapping("commends")
    public PageInfo queryCommend(TravelCommendQueryObject qo){
        //按照推荐时间来排序 倒序
        qo.setOrderBy("tc.schedule desc");
        //获取今天之前的数据
        qo.setBeforToday(true);
        return travelCommendService.queryApp(qo);
    }

    /**
     * 根据关键字查询游记内容
     * @param qo
     * @return
     */
    @GetMapping("{keyword}/search")
    public PageInfo selectTravelByKeyword(TravelQueryObject qo){
        //将状态设置为已发布
        qo.setState(Travel.STATE_RELEASE);
        PageInfo<Travel> query = travelService.query(qo);
        return query;
    }


    @GetMapping("{keyword}/countNum")
    public int selectCountByKeyword(TravelQueryObject qo){
        //将状态设置为已发布
        qo.setState(Travel.STATE_RELEASE);
        int countNum=travelService.selectCountByKeyword(qo);
        return countNum;
    }

    /**
     * 获取某个游记下的所有评论
     */
    @GetMapping("/{travelId}/comments")
    public PageInfo getCommentById(TravelReviewQueryObject qo){
        qo.setOrderBy("travelreview.createTime");
        return travelReviewService.getCommentById(qo);
    }

    /**
     * 新增单条游记评论
     * @param travelReview
     * @return
     */
    @PostMapping("/{travel.id}/comments")
    public JSONResult saveComment(TravelReview travelReview){
        travelReviewService.save(travelReview);
        return new JSONResult();
    }

    @PutMapping("watchNum")
    public JSONResult updateWatchNum(int userId,int num){
        travelService.updateWatchNum(userId,num);
        return new JSONResult();
    }

    @PutMapping("visitNum")
    public JSONResult updateVisitNum(int userId,int num){
        travelService.updateVisitNum(userId,num);
        return new JSONResult();
    }
}